//
//  RecommendCycleView.swift
//  DouYuZB
//
//  Created by James on 2017/10/19.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit

let kCycleViewId = "kCycleViewId"

class RecommendCycleView: UIView {
    var timer:Timer?
    var cycleModels:[CycleModel]?{
        didSet{
            collectionView.reloadData()
            //设置 pagecontrol个数
            pageControl.numberOfPages = cycleModels?.count ?? 0
            
//            let index = (cycleModels?.count ?? 0);
            
//            let indexPath = IndexPath(item: index*10 , section: 0)
//            collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
            
            removeTimer()
            addTimer()
        }
    }
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    @IBOutlet weak var pageControl: UIPageControl!
    override func awakeFromNib() {
        autoresizingMask = UIViewAutoresizing.init(rawValue: 0)
        
        collectionView.register(UINib(nibName: "CollectionCycleCell", bundle: nil), forCellWithReuseIdentifier: kCycleViewId);
    }
    
    override func layoutSubviews() {
        
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = collectionView.bounds.size
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .horizontal
        layout.minimumInteritemSpacing = 0
    }
    
    class func recommendCycleView() -> RecommendCycleView {
        return Bundle.main.loadNibNamed("RecommendCycleView", owner: nil, options: nil)?.first as! RecommendCycleView
    }

}

extension RecommendCycleView : UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (cycleModels?.count ?? 0)*10000
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:CollectionCycleCell = collectionView.dequeueReusableCell(withReuseIdentifier: kCycleViewId, for: indexPath) as! CollectionCycleCell;
    
        let cycleModel = cycleModels![indexPath.row % cycleModels!.count]
        
        cell.model = cycleModel
        
        return cell
    }
}

extension RecommendCycleView : UICollectionViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetX = scrollView.contentOffset.x + scrollView.bounds.width*0.5
        //滚到一半就切换到下一页
        let page = Int(offsetX/scrollView.bounds.width) % (cycleModels?.count ?? 1)
        
        pageControl.currentPage = page
    }
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        removeTimer()
    }
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        addTimer()
    }
}

extension RecommendCycleView {
    private func addTimer()  {
        timer = Timer(timeInterval: 3.0, target: self, selector: #selector(self.scrollToNext), userInfo: nil, repeats: true)
        RunLoop.current.add(timer!, forMode: .commonModes)
    }
    
    private func removeTimer()  {
        timer?.invalidate()
        timer = nil
    }
    
    @objc func scrollToNext(){
        let currentX = collectionView.contentOffset.x
        let offsetX = currentX + collectionView.bounds.width
        collectionView.setContentOffset(CGPoint(x:offsetX,y:0), animated: true)
    }
}
